// pages/address/NewAddress/NewAddress.js

var http = require('../../../utils/server.js');
var util = require('../../../utils/util.js');

Page({
  data: {
    chooseLoc: {},
    consignee: "",
    phone: "",
    street: "",
    address: "",
    region: [],
    id: 0
  },

  onLoad: function(option) {
    var that = this
    console.log('收到参数', option)
    if (option.id) {
      that.data.chooseLoc = {
        longitude: option.position_lng,
          latitude: option.position_lat
      },
      that.setData({
        id: option.id,        
        street: option.street,
        address: option.address,
        phone: option.phone,
        consignee: option.consignee,
        region: [option.province, option.city, option.county],
      })
    } else {
      wx.setNavigationBarTitle({
        title: '新增地址'
      })
    }
  },
  gcj02tobd09: function(lng, lat) {
    var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    var lat = +lat;
    var lng = +lng;
    var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
    var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
    var bd_lng = z * Math.cos(theta) + 0.0065;
    var bd_lat = z * Math.sin(theta) + 0.006;
    return [bd_lng, bd_lat]
  },
  chooseLocation: function() {
    var that = this;
    wx.chooseLocation({
      type: 'gcj02',
      success: function(res) {
        console.log('地图选址', res)
        //没地名 去百度搜一个
        var baiduCoordinate = that.gcj02tobd09(res.longitude, res.latitude)
        wx.request({
          url: 'https://api.map.baidu.com/geocoder/v2/?ak=aCb2mDllVihBtpCywqagyyVzUz4wQb31&output=json&callback=renderReverse&location=' + baiduCoordinate[1] + ',' + baiduCoordinate[0] + '&output=json&pois=1',
          data: {},
          header: {
            'Content-Type': 'application/json'
          },
          success: function(ops) {
            var loc = JSON.parse(ops.data.substr(29, ops.data.length - 30))
            console.log(loc)
            res.city = loc.result.addressComponent.city
            res.area = loc.result.addressComponent.district
            res.province = loc.result.addressComponent.province
            res.name = res.name ? res.name : loc.result.pois[0].addr
            that.data.chooseLoc = res
            that.setData({
              region: [res.province, res.city, res.area],
              // city: res.city,
              // county: res.area,
              // province: res.province,
              // value: [that.data.provinces.indexOf(res.province), that.data.citys.indexOf(res.city),0],
              // values: [that.data.provinces.indexOf(res.province), that.data.citys.indexOf(res.city), that.data.countys.indexOf(res.area)]
            })
          }
        })
        res.latitude = parseFloat(res.latitude).toFixed(6)
        res.longitude = parseFloat(res.longitude).toFixed(6)
        that.data.chooseLoc = res
        that.setData({
          street: res.name,
          address: res.address
        })
      }
    })
  },
  bindConsignee: function(e) {
    this.setData({
      consignee: e.detail.value
    })
  },
  bindPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  bindStreet: function(e) {
    this.setData({
      street: e.detail.value
    })
  },

  bindRegionChange: function(e) {
    this.setData({
      region: e.detail.value
    })
  },
  formSubmit: function() {
    var that = this
    var postData = {
      id: that.data.id,
      url: "address/editAddress",
      consignee: that.data.consignee,
      phone: that.data.phone,
      address: that.data.address,
      province: that.data.region[0],
      city: that.data.region[1],
      county: that.data.region[2],
      street: that.data.street,
      lng: that.data.chooseLoc.longitude,
      lat: that.data.chooseLoc.latitude,
    }
    http.post(postData, function(data) {
      util.showSuccess(that.data.id == 0 ? '新增成功' : '修改成功', function() {
        var pages = getCurrentPages();
        if (pages.length > 1) {
          var prePage = pages[pages.length - 2];
          prePage.getData()
        }
        wx.navigateBack({
          delta: 1
        })
      })
    })
  },
})